package com.jxdinfo.hussar.activitidemo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.plugins.Page;
import com.jxdinfo.hussar.activitidemo.bean.TdbYjMCallRecord;
import com.jxdinfo.hussar.activitidemo.dao.TdbYjMCallRecordMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2019-05-23
 */
@Service
@Transactional
public class TdbYjMCallRecordServiceImpl extends ServiceImpl<TdbYjMCallRecordMapper, TdbYjMCallRecord> implements ITdbYjMCallRecordService {

    @Autowired
    private TdbYjMCallRecordMapper mapper;

    /**
     * 更新来电记录
     * @param callRecord
     */
    @Override
    public void updateCallRecord(TdbYjMCallRecord callRecord) {
        mapper.updateCallRecord(callRecord);
    }

    /**
     * 批量更新
     * @param recordJsonObj
     */
    @Override
    public void updateBetchCallRecord(String recordJsonObj) {
        List<TdbYjMCallRecord> list = JSON.parseObject(recordJsonObj, new TypeReference<List<TdbYjMCallRecord>>() {});
        for (TdbYjMCallRecord l : list){
            mapper.updateCallRecord(l);
        }
    }

    /**
     * 查询来电记录列表
     * @param page
     * @param info
     * @return
     */
    @Override
    public Page<TdbYjMCallRecord> selectByCondition(Page<TdbYjMCallRecord> page, TdbYjMCallRecord info) {
        List<TdbYjMCallRecord> list = mapper.selectByCondition(page, info);
        page.setRecords(list);
        Integer count = mapper.selectCountByCondition(info);
        page.setTotal(count);
        return page;
    }

    @Override
    public void insertCallRecord(TdbYjMCallRecord callRecord) {
        mapper.insertCallRecord(callRecord);
    }

    @Override
    public Integer getNotDealCallNum() {
        TdbYjMCallRecord info = new TdbYjMCallRecord();
        //获取未处理状态的记录条数
        info.setsStatus(0);
        Integer count = mapper.selectCountByCondition(info);
        return count;
    }

    /**
     * 更新状态前，判断是否未处理
     * 如果未处理予以更新
     * true:更新
     * false:通知用户应急已被处理
     * @param callRecord
     */
    @Override
    public Boolean updateAndCheckCallRecord(TdbYjMCallRecord callRecord) {
        TdbYjMCallRecord record = mapper.getCallRecordById(callRecord.getsId());
        if (record.getsStatus()!=0){
            //说明不是未处理数据
            return false;
        }else {
            mapper.updateCallRecord(callRecord);
            return true;
        }
    }
}
